package userService

import (
	"chart.zozoo.net/app/models"
	"chart.zozoo.net/app/utils/jwt"
	"chart.zozoo.net/core/mysql"
	"errors"
)

func Login(p *RegisterParams) (token string, err error) {

	u := new(models.Users)

	//查询用户是否存在
	res := mysql.MysqlClient.Select("uuid", "password").First(u)
	if res.Error != nil {
		return "", res.Error
	}

	//比较密码
	err = u.ComparePwd(u.Password, p.Password)
	if err != nil {
		return "", errors.New("password error")
	}

	//生成token
	token, err = jwt.GenerateToken(u.Uuid)
	if err != nil {
		return "", err
	}

	return token, nil
}
